﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using ISS.CaseStudy.DBUtility;
using ISS.CaseStudy.Entities;

namespace ISS.CaseStudy.DAL
{
    public static class IndustryDLL
    {
        public static DataSet GetAllInfos()
        {
            string sql = "select * from IndustryManage order by ModifyTime desc";
            DataSet dataSet = Database.SpExecuteDataSet(sql);
            return dataSet;
        }

        public static IndustryInfo GetInfo(string id)
        {
            string sql = "select * from IndustryManage where [Id]=@id";
            SqlParameter[] paramCollection = { new SqlParameter("@id", id) };
            DataSet dataSet = Database.SpExecuteDataSet(sql, paramCollection);
            DataTable dataTable = dataSet.Tables[0];
            if (dataTable.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dataTable.Rows[0];

            IndustryInfo industryInfo = new IndustryInfo();
            industryInfo.Id = id;
            industryInfo.Value = row["Value"].ToString();

            if (!string.IsNullOrEmpty(row["CreateTime"].ToString()))
            {
                industryInfo.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
            }

            if (!string.IsNullOrEmpty(row["ModifyTime"].ToString()))
            {
                industryInfo.ModifyTime = DateTime.Parse(row["ModifyTime"].ToString());
            }

            return industryInfo;
        }

        public static bool Update(IndustryInfo industryInfo)
        {
            bool b = false;
            string sql = "UPDATE IndustryManage SET [Value]=@Value, [ModifyTime]=@ModifyTime where ([Id]=@Id)";
            SqlParameter[] paramCollection = { new SqlParameter("@Value", industryInfo.Value), 
                                                 new SqlParameter("@ModifyTime", industryInfo.ModifyTime.ToString()),
                                                 new SqlParameter("@Id", industryInfo.Id)};
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }

        public static bool Insert(IndustryInfo industryInfo)
        {
            bool b = false;
            string sql = "insert into IndustryManage([Value], [CreateTime], [ModifyTime]) values (@Value, @CreateTime, @ModifyTime)";
            SqlParameter[] paramCollection = { new SqlParameter("@Value", industryInfo.Value),
                                                 new SqlParameter("@CreateTime", industryInfo.CreateTime.ToString()),
                                                 new SqlParameter("@ModifyTime", industryInfo.ModifyTime.ToString())};
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }

        public static bool Delete(string id)
        {
            bool b = false;
            string sql = "delete from IndustryManage where ([Id]=@Id)";
            SqlParameter[] paramCollection = { new SqlParameter("@Id", id) };
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }
    }
}
